$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'member/userapplycommission/list',
        datatype: "json",
        colModel: [
            { label: '操作', width: 80,align:'center',
                formatter:function(value, options, row){
                    var actions = [];
                    if(row.status == 1){
                        actions.push('<a href="javascript:void(0);" onclick="return vm.audit('+ options.rowId +');">审核</a>&nbsp;&nbsp;|') ;
                        actions.push('<a href="javascript:void(0);" onclick="return vm.reject('+ options.rowId +');">驳回</a>') ;
                    }else if(row.status == 2){
                        actions.push('<a href="javascript:void(0);" onclick="return vm.pay('+ options.rowId +');">支付</a>&nbsp;&nbsp;|') ;
                        actions.push('<a href="javascript:void(0);" onclick="return vm.reject('+ options.rowId +');">驳回</a>') ;
                    }else{
                        actions.push('—') ;
                    }

                    return actions.join('  ');
                }
            },
            { label: '申请ID', name: 'applyId', index: 'apply_id', width: 50, key: true, align:'center'},
            { label: '会员ID', name: 'memberId', index: 'member_id', width: 50, hidden:true },
            { label: '会员名',  name: 'memberName', width: 150, formatter: function (value, options, row) {
                var valueText = "";
                var userData = $("#" + options.gid).jqGrid('getGridParam', 'userData');
                $.each(userData.memberNameList, function (index, module) {
                    if (module.memberId == row.memberId) {
                        // valueText = module.name;
                        valueText = '<a href="javascript:;" onclick="return showDetail('+ options.rowId +');">'+ module.name +'</a>';
                        return false;
                    }
                });
                return valueText;
            }},
            { label: '申请金额', name: 'amount', index: 'amount', width: 80, align:'right'},
            { label: '申请状态', name: 'status', index: 'status', width: 50, align:'center',hidden:true },
            { label: '申请状态',  width: 50, align:'center', formatter: function (value, options, row) {
                var valueText = "";
                // var userData = $("#jqGrid").jqGrid('getGridParam', 'userData');
                // $.each(userData.userApplyCommissionStatusEnum, function (index, module) {
                //     if (module.id == row.status) {
                //         valueText = module.describe;
                //         return false;
                //     }
                // });
                return valueText;
            }},
            { label: '申请时间', name: 'applyTime', index: 'apply_time', width: 120, align:'center' },
            { label: '审核时间', name: 'auditTime', index: 'audit_time', width: 120, align:'center' },
            { label: '审核人', name: 'auditName', index: 'audit_name', width: 80, align:'center',
                formatter: function (value, options, row) {
                    var valueText = "";
                    var userData = $("#jqGrid").jqGrid('getGridParam', 'userData');
                    $.each(userData.auditoNameList, function (index, module) {
                        if (module.userId ==value) {
                            valueText = module.username;
                            return false;
                        }
                    });
                    return valueText;
                }
            },
            { label: '审核结果信息', name: 'resultInfo', index: 'result_info', width: 80 }
        ],
        viewrecords: true,
        height: window.innerHeight - 120,
        rowNum: 25,
        rowList : [25,50,100],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount",
            userdata: "userData"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            // vm.e.userApplyCommissionStatusEnum = $("#jqGrid").jqGrid('getGridParam', 'userData').userApplyCommissionStatusEnum;
        }
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        title: null,
        userApplyCommission: {},
        userShare:{},
        memberName:null,
        q:{
            memberName: ''
        },
        e:{
            userApplyCommissionStatusEnum:[]
        },
    },
    methods: {
        query: function () {
            vm.reload();
        },
        audit:function(applyId){
            confirm('确定审核通过选中的申请记录？', function(){
                $.ajax({
                    type: "GET",
                    url: baseURL + "member/userapplycommission/audit",
                    contentType: "application/json",
                    data: {'applyId':applyId},
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(index){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        reject:function(applyId){
            confirm('确定驳回选中的申请记录？', function(){
                $.ajax({
                    type: "GET",
                    url: baseURL + "member/userapplycommission/reject",
                    contentType: "application/json",
                    data: {'applyId':applyId},
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(index){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            })
        },
        pay:function(applyId){
            confirm('确定支付选中的申请记录？', function(){
                $.ajax({
                    type: "GET",
                    url: baseURL + "member/userapplycommission/pay",
                    contentType: "application/json",
                    data: {'applyId':applyId},
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(index){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function(applyId){
            $.get(baseURL + "member/userapplycommission/info/"+applyId, function(r){
                vm.userApplyCommission = r.userApplyCommission;
                vm.userShare = r.userShare;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                page:page,
                postData:{
                    'memberName': vm.q.memberName,
                }
            }).trigger("reloadGrid");
        },
        /* 查询重置 */
        reset: function () {
            vm.q.memberName = '';
            vm.reload();
        },
    }
});

/**
 * 展示佣金详情
 * @param memberId
 */
function showDetail(rowId) {
    var rowData = $("#jqGrid").jqGrid('getRowData', rowId);
    vm.getInfo(rowData.applyId);
    vm.memberName = $(rowData.memberName).html()
    layer.open({
        type: 1,
        skin: 'layui-layer-molv',
        title: "会员佣金",
        area: ['420px', '500px'],
        shadeClose: false,
        content: jQuery("#commissionDetailLayer")
    });
}